package com.jsh.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsh.erp.datasource.entities.Car;
import com.jsh.erp.datasource.entities.User;
import com.jsh.erp.datasource.vo.CarVo;
import com.jsh.erp.datasource.vo.PageVo;
import com.jsh.erp.service.car.CarService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping(value = "/car")
public class CarController {

    @Autowired
    CarService service;

    @Autowired
    UserService userService;

    @PostMapping(value = "/save")
    public BaseResponseInfo save(@RequestBody CarVo params) {
        BaseResponseInfo responseInfo = new BaseResponseInfo();
        User user;
        try {
            user = userService.getCurrentUser();
        } catch (Exception e) {
            responseInfo.code = 500;
            return responseInfo;
        }
        Car car = params.getId() != null ? service.getById(params.getId()) : new Car();
        car.setCarNumber(params.getCarNumber());
        car.setDriver(params.getDriver());
        car.setMobile(params.getMobile());
        car.setImage(params.getImage());
        car.setUpdateTime(new Date());
        car.setUpdateBy(user.getLoginName());
        car.setCarId(params.getCarId());
        if (params.getId() == null) {
            car.setCreateTime(new Date());
            car.setCreateBy(user.getLoginName());
        }
        if (service.saveOrUpdate(car)) {
            responseInfo.code = 200;
            responseInfo.data = car;
        } else {

            responseInfo.code = 500;
        }
        return responseInfo;
    }

    @PostMapping(value = "/page")
    public BaseResponseInfo page(@RequestBody PageVo pageVo) {

        BaseResponseInfo responseInfo = new BaseResponseInfo();
        User user;
        try {
            user = userService.getCurrentUser();
        } catch (Exception e) {
            responseInfo.code = 500;
            return responseInfo;
        }
        LambdaQueryWrapper<Car> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtil.isNotEmpty(pageVo.getKeyword())) {
            queryWrapper.and(q -> q.like(Car::getCarNumber, pageVo.getKeyword()).or().like(Car::getMobile, pageVo.getKeyword()));
        }
        queryWrapper.eq(Car::getTenantId, user.getTenantId());
        IPage<Car> iPage = service.page(new Page<>(pageVo.getPage(), pageVo.getPageSize()), queryWrapper);
        responseInfo.data = new Page<Car>(iPage.getCurrent(), iPage.getSize(), iPage.getTotal()).setRecords(iPage.getRecords());
        responseInfo.code = 200;
        return responseInfo;
    }

    @PostMapping(value = "/list")
    public BaseResponseInfo list() {

        BaseResponseInfo responseInfo = new BaseResponseInfo();
        User user;
        try {
            user = userService.getCurrentUser();
        } catch (Exception e) {
            responseInfo.code = 500;
            return responseInfo;
        }
        LambdaQueryWrapper<Car> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Car::getTenantId, user.getTenantId());
        responseInfo.data = service.getBaseMapper().selectList(queryWrapper);
        responseInfo.code = 200;
        return responseInfo;
    }
}
